﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using KingSite.Purview.WebUI.Models;
using KingSite.Purview.MVC;

namespace KingSite.Purview.WebUI.Controllers {
    public class KSPUserController : PurviewController {
        
        MembershipExProvider _provider = (MembershipExProvider) Membership.Provider;

        RoleProvider _roleProvider = Roles.Provider;

        public ActionResult Index(string userName) {
            ViewBag.Message = "查看用户";
            int total;
            MembershipUserCollection list = _provider.FindUsersByName(userName, 1, 100, out total);
            MembershipUser user = list[userName];
            ViewData.Model = user;
            string[] allRoles = _roleProvider.GetAllRoles();
            Dictionary<string, bool> roles = new Dictionary<string, bool>();
            foreach(string role in allRoles){
                roles.Add(role, false);    
            }            
            string[] inRoles = Roles.GetRolesForUser(userName);
            foreach (string role in inRoles) {
                roles[role] = true;
            }
            ViewData["InRoles"] = roles;
            ViewData["UserName"] = userName;
            return View();
        }

        public ActionResult List() {
            ViewBag.Message = "用户列表";
            int total;
            MembershipUserCollection list = _provider.GetAllUsers(1, 10, out total);
            ViewData.Model = list;
            string[] roles = Roles.GetRolesForUser("jerry");
            return View();
        }

        public ActionResult Create() {
            ViewBag.PasswordLength = _provider.MinRequiredPasswordLength;
            return View();
        }

        [HttpPost]
        public ActionResult Create(RegisterModel model) {
            if (ModelState.IsValid) {
                // 尝试注册用户
                MembershipCreateStatus createStatus;
                _provider.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
                if (createStatus == MembershipCreateStatus.Success) {
                    return RedirectToAction("Index", new { userName = model.UserName });
                }
                else {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // 如果我们进行到这一步时某个地方出错，则重新显示表单
            ViewBag.PasswordLength = _provider.MinRequiredPasswordLength;
            return View(model);
        }

        public ActionResult DeleteFromRole(string userName, string roleName) {
            _roleProvider.RemoveUsersFromRoles(new string[] { userName }, new string[] { roleName });
            return RedirectToAction("Index", new { userName = userName });
        }

        public ActionResult AddToRole(string userName, string roleName) {
            _roleProvider.AddUsersToRoles(new string[] { userName }, new string[] { roleName });
            return RedirectToAction("Index", new { userName = userName });
        }

        public ActionResult Lock(string userName) {
            _provider.LockUser(userName);
            return RedirectToAction("Index", new { userName = userName });
        }

        public ActionResult UnLock(string userName) {
            _provider.UnlockUser(userName);
            return RedirectToAction("Index", new { userName = userName });
        }

        public ActionResult Delete(string userName) {
            _provider.DeleteUser(userName, false);
            return RedirectToAction("List");
        }

        public ActionResult ResetPassword(string userName, string password) {
            bool b = _provider.ResetPwd(userName, password);
            return RedirectToAction("ResetPasswordSuccessful", new { userName = userName });
        }

        public ActionResult ResetPasswordSuccessful(string userName) {
            ViewData["userName"] = userName;
            return View();
        }
    }
}
